'use strict';

stareal
    .controller("ResetPasswordController2", function ($scope, $api, $stateParams, $alert, $document, localStorageService, $state, $interval) {
        $scope.telphone_no = "";
        $scope.accessToken = "";
        $scope.paracont = "获取验证码";
        $scope.code = "";
        $scope.password = "";
        $scope.password2 = "";
        $scope.sendCodeStatus=false;
        $scope.$watch('code',function (newValue, oldValue) {
            // console.log($scope.code);
            if( $scope.code!=''&&$scope.code!=null){
                $scope.isActive =  true;
            }else{
                $scope.isActive =  false;
            }
        });
        $scope.login = {
            sendCode:function () {
                var second = 60;
                var timerHandler = undefined;
                if($scope.sendCodeStatus){
                    return;
                }
                if (!this.validatemobile($scope.telphone_no)) {
                    return;
                }
                // 验证码
                $api.get("app/login/code/retrieve", {mobile:$scope.telphone_no, type: "0"})
                    .then(function (ret) {
                        if (ret.retCode == "0") {
                            $alert.show("验证码已发送!");
                            timerHandler = $interval(function () {
                                if (second <= 0) {
                                    $interval.cancel(timerHandler);
                                    timerHandler = undefined;
                                    second = 60;
                                    $scope.paracont = "重发";
                                    $scope.sendCodeStatus=false;
                                } else {
                                    $scope.sendCodeStatus=true;
                                    $scope.paracont = second + "秒";
                                    second--;

                                }
                            }, 1000, 100)
                            localStorageService.set('code_token', ret.accessToken);
                            $scope.accessToken = ret.accessToken;
                        } else {
                            $alert.show("验证码发送失败，请稍后重试!");
                        }
                    });

                // if (timerHandler) {
                //     return;
                // }

            },
            // next:function (){
            //     if (!this.validatemobile($scope.telphone_no)) {
            //         return;
            //     }
            //     if (!localStorageService.get('code_token')) {
            //         $alert.show("请先获取验证码！");
            //         return false;
            //     }
            //     if (!$scope.code) {
            //         $alert.show("验证码不能为空！");
            //         return false;
            //     }
            //     localStorageService.set("rese_tel",$scope.telphone_no)
            //     localStorageService.set("rese_code",$scope.code)
            //     $api.get("app/login/code/retrieve", {mobile:$scope.telphone_no, type: "1", code:$scope.code, smsAccessToken:$scope.accessToken})
            //         .then(function (ret) {
            //             if (ret.retCode == "0") {
            //                 $state.go('main.reset_password_2',{});
            //             } else {
            //                 $alert.show("验证码错误");
            //             }
            //         },function (err) {
            //             $alert.show(err)
            //         });
            // },
            validatemobile:function (mobile) {
                if(!mobile){
                    $alert.show("请输入手机号")
                    return false
                }
                if (mobile.toString().length == 0) {
                    $alert.show('请输入手机号码！');
                    return false;
                }
                if (mobile.toString().length != 11) {
                    $alert.show('请输入11位手机号码！');
                    return false;
                }
                var myreg = /^1[3|4|5|6|7|8|9][0-9]{9}$/; //验证规则
                if (!myreg.test(mobile)) {
                    $alert.show('请输入有效的手机号码！');
                    return false;
                }
                return true;
            },
            save:function () {
                if (!this.validatemobile($scope.telphone_no)) {
                    return;
                }
                if (!localStorageService.get('code_token')) {
                    $alert.show("请先获取验证码！");
                    return false;
                }
                if (!$scope.code) {
                    $alert.show("验证码不能为空！");
                    return false;
                }
                $scope.code=$('#code').val();
                localStorageService.set("rese_tel",$scope.telphone_no);
                localStorageService.set("rese_code",$scope.code);
                if(!localStorageService.get("rese_tel")||!localStorageService.get("rese_code")||!localStorageService.get('code_token')){
                    $alert.show("请填写正确的手机号或验证码");
                    return false;
                }
                if($scope.password==null&&$scope.password==''&&$scope.password==undefined){
                    $alert.show("密码不能为空");
                    return false;
                }
                if($scope.password.length<6){
                    $alert.show("请设置大于6位数密码")
                    return false;
                }
                if($scope.password.length>18){
                    $alert.show("请设置小于18位数密码")
                    return false;
                }
                // if($scope.password!==$scope.password2){
                //     $alert.show("两次输入的密码不一致");
                //     return false;
                // }
                var regExp=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/;
                if (!regExp.test($scope.password)){
                    $alert.show("请设置6~18位字母和数字组合的密码")
                    return false;
                }
                var _params = {
                    mobile: localStorageService.get("rese_tel"),
                    code: localStorageService.get("rese_code"),
                    password:$scope.password,
                    smsToken: localStorageService.get('code_token')
                };
                $api.post("app/login/user/forget",_params)
                    .then(function (ret) {
                        $alert.show("修改成功");
                        $state.go('main.login',{})
                        localStorageService.set('token',null);
                        localStorageService.set('isbind','0');
                        window.location.href='#/main/login/'
                    },function (err) {
                        $alert.show(err)
                    })
            }
        };
        // $scope.password = "";
        // $scope.password2 = "";
        // $scope.login = {
        //     save:function () {
        //         if(!localStorageService.get("rese_tel")||!localStorageService.get("rese_code")||!localStorageService.get('code_token')){
        //             $alert.show("请填写正确的手机号或验证码");
        //             return false;
        //         }
        //         if($scope.password==null&&$scope.password==''&&$scope.password==undefined){
        //             $alert.show("密码不能为空");
        //             return false;
        //         }
        //         if($scope.password.length<6){
        //             $alert.show("请设置大于6位数密码")
        //             return false;
        //         }
        //         if($scope.password.length>18){
        //             $alert.show("请设置小于18位数密码")
        //             return false;
        //         }
        //         if($scope.password!==$scope.password2){
        //             $alert.show("两次输入的密码不一致");
        //             return false;
        //         }
        //         var regExp=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/;
        //         if (!regExp.test($scope.password)){
        //             $alert.show("请设置6~18位字母和数字组合的密码")
        //             return false;
        //         }
        //         var _params = {
        //             mobile: localStorageService.get("rese_tel"),
        //             code: localStorageService.get("rese_code"),
        //             password:$scope.password,
        //             smsToken: localStorageService.get('code_token')
        //         };
        //         $api.post("app/login/user/forget",_params)
        //             .then(function (ret) {
        //                 $alert.show("修改成功");
        //                 $state.go('main.login',{good_id:''})
        //             },function (err) {
        //                 $alert.show(err)
        //             })
        //     }
        // }
    });